﻿Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports System.Xml.Linq
Imports System.Linq
Imports Aricie.DNN.Services.UpgradeSystem
Imports Aricie.DNN.Services.UpgradeSystem.Actions.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Business

Namespace UpgradeSystem
    Public Class UpgradeTo160ActionsProvider
        Implements IUpgradeActionsProvider(Of LuceneSearchController)


        Public Function GetActions() As UpgradeActionsCollection Implements IUpgradeActionsProvider(Of LuceneSearchController).GetActions
            Dim UpgradeTo160 = New UpgradeActionsCollection(New Version(1, 6, 0))

            UpgradeTo160.UpgradeModuleSettingsActions.Add(New UpgradeModuleSettingsAction(Of LuceneResultsSettings) With {.Name = "Modification de tous les chemins de contrôles", .UpgradeOperation = AddressOf Me.MoveControlsLocation})

            Return (UpgradeTo160)
        End Function


        Private Function MoveControlsLocation(TargetVersion As Version, XD As XDocument, ScopeInfo As ScopeInformation) As XDocument

            Dim FilterControlValue = (From nodes In XD.Descendants("FilterControl") Select nodes).Select(Function(n) n.Value).FirstOrDefault
            If (Not String.IsNullOrEmpty(FilterControlValue)) Then
                If FilterControlValue <> FilterControl.CustomFilter.ToString() Then

                    For Each xNode In XD.Descendants("FilterToLoad")
                        Dim LastSlash = xNode.Value.LastIndexOf("/")
                        xNode.Value = "./Controls/AdvancedFilters" & xNode.Value.Substring(LastSlash)
                    Next

                Else
                    For Each xNode In XD.Descendants("FilterToLoad")
                        If (xNode.Value.Contains("IBSSearchFilter.ascx")) Then
                            ' changer cette migration
                            xNode.Value = "./Controls/AdvancedFilters/CommonFilter.ascx"
                        End If

                    Next

                End If
            End If

            Return XD
        End Function

    End Class
End Namespace